package com.zzedu.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.utils.DateUtils;
import com.zzedu.common.utils.ServletUtils;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.poi.ExcelUtil;
import com.zzedu.domain.ZzStudent;
import com.zzedu.dto.ZzStudentDTO;
import com.zzedu.params.RenewParams;
import com.zzedu.service.IZzStudentService;
import com.zzedu.vo.ZzStudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 学生信息Controller
 *
 * @author Mr.Wang
 * @date 2025-02-16
 */
@RestController
@RequestMapping("/school/student")
public class ZzStudentController extends BaseController {
    @Autowired
    IZzStudentService zzStudentService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:student:query')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody ZzStudentDTO zzStudent) {
        IPage<ZzStudent> page = zzStudentService.page(zzStudent.getPageDTO(), zzStudentService.buildQuery(zzStudent));
        return getDataTable(page, zzStudentService.convertVO(page.getRecords()));
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:student:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public void export(ZzStudentDTO zzStudent) {
        List<ZzStudent> list = zzStudentService.list(zzStudentService.buildQuery(zzStudent));
        ExcelUtil<ZzStudentVO> util = new ExcelUtil<ZzStudentVO>(ZzStudentVO.class);
        util.exportExcel(ServletUtils.getResponse(), zzStudentService.convertVO(list), "学生信息数据");
    }


    /**
     * 新增保存学生信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:student:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    public AjaxResult addSave(@Validated @RequestBody ZzStudentDTO zzStudentDTO) {
        zzStudentDTO.setTel(StringUtils.trimToEmpty(zzStudentDTO.getTel()));
        return toAjax(zzStudentService.save(zzStudentDTO));
    }


    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:student:query')")
    @RequestMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ZzStudent zzStudent = zzStudentService.getById(id);
        ZzStudentVO zzStudentVO = zzStudentService.convertVO(zzStudent);
        JSONObject jsonObject = (JSONObject) JSON.toJSON(zzStudentVO);

        jsonObject.put("validFrom", null == zzStudentVO.getValidFrom() ? "" : zzStudentVO.getValidFrom().format(DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD_HH_MM_SS)));
        jsonObject.put("validTo", null == zzStudentVO.getValidTo() ? "" : zzStudentVO.getValidTo().format(DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD_HH_MM_SS)));
        jsonObject.put("provinceAndCityAndDistrict", Lists.newArrayList(zzStudentVO.getProvince(), zzStudentVO.getCity(), zzStudentVO.getDistrict()));
        return success(jsonObject);
    }


    /**
     * 修改保存学生信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:student:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @RequestMapping("/edit")
    public AjaxResult editSave(@Validated @RequestBody ZzStudentDTO zzStudentDTO) {
        zzStudentDTO.setTel(StringUtils.trimToEmpty(zzStudentDTO.getTel()));
        zzStudentDTO.setValidFrom(null);
        return toAjax(zzStudentService.save(zzStudentDTO));
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:student:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(zzStudentService.removeBatchByIds(ids));
    }


    /**
     * 续费
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:student:renew')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @RequestMapping("/renew")
    public AjaxResult renew(@RequestBody RenewParams renewParams) {
        return toAjax(zzStudentService.renew(renewParams.getId(), renewParams.getMemberType(), renewParams.getStartDate()));
    }
}
